package com.yuwenxin.core;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBUtil {
    private static DataSource ds;
    static {
        ds = new ComboPooledDataSource();  //不填入任何参数表示使用默认的config文件
    }
    public static DataSource getDataSource(){
        return ds;
    }
    public static Connection getConnection() throws Exception{
        return ds.getConnection();
    }

    public static void close(Connection con, PreparedStatement pst, ResultSet rs) throws Exception{
        if(rs!=null){
            rs.close();
        }
        if(pst!=null){
            pst.close();
        }
        if(con!=null){
            con.close();
        }
    }

    public static void close(Connection con, Statement pst, ResultSet rs) throws Exception{
        if(rs!=null){
            rs.close();
        }
        if(pst!=null){
            pst.close();
        }
        if(con!=null){
            con.close();
        }
    }
    public static void close(Connection con, Statement pst) throws Exception{
        if(pst!=null){
            pst.close();
        }
        if(con!=null){
            con.close();
        }
    }
}
